class NotificationServices::SlackService < NotificationService
  Label = "slack"
  Fields += [
    [:service_url, {
      :placeholder => 'Slack Hook URL (https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXX)',
      :label => 'Hook URL'
    }]
  ]

  def check_params
    if Fields.detect {|f| self[f[0]].blank? }
      errors.add :base, "You must specify your Slack Hook url."
    end
  end

  def message_for_slack(problem)
    "[#{problem.app.name}][#{problem.environment}][#{problem.where}]: #{problem.error_class} #{problem_url(problem)}"
  end

  def post_payload(problem)
    {
      :text => "<#{problem_url(problem)}|Errbit - #{problem.app.name}: #{problem.error_class}>",
      :attachments => [
        {
           :fallback => message_for_slack(problem),
           :pretext => "<#{problem_url(problem)}|Errbit - #{problem.app.name}: #{problem.error_class}>",
           :text => "<#{problem_url(problem)}|Errbit - #{problem.app.name}: #{problem.error_class}>",
           :color => "#D00000",
           :fields => [
              {
                 :title => "Environment",
                 :text  => problem.environment,
                 :value => problem.environment,
                 :short => false
              },
              {
                 :title => "Location",
                 :text => problem.where,
                 :value => problem.where,
                 :short => false
              },
              {
                 :title => "Message",
                 :text => problem.message.to_s,
                 :value => problem.message.to_s,
                 :short => false
              },
              {
                 :title => "First Noticed",
                 :text => problem.first_notice_at,
                 :value => problem.first_notice_at,
                 :short => false
              },
              {
                 :title => "Last Noticed",
                 :text => problem.last_notice_at,
                 :value => problem.last_notice_at,
                 :short => false
              },
              {
                 :title => "Times Occurred",
                 :text => problem.notices_count,
                 :value => problem.notices_count,
                 :short => false
              }
           ]
        }
      ]
    }.to_json
  end

  def create_notification(problem)
    HTTParty.post(service_url, :body => post_payload(problem), :headers => { 'Content-Type' => 'application/json' })
  end

  def configured?
    service_url.present?
  end
end
